{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# NumPy库快速入门\n",
    "叶璨铭, 12011404@mail.sustech.edu.cn\n",
    "本笔记参考 [NumPy中文网](https://www.numpy.org.cn/user/quickstart.html#%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "NumPy的主要对象是同构多维数组。它是一个元素表（通常是数字），所有类型都相同，由非负整数元组索引。在NumPy维度中称为 轴 。\n",
    "\n",
    "3D空间中的点的坐标[1, 2, 1]具有一个轴。该轴有3个元素，所以我们说它的长度为3.\n",
    "[[ 1., 0., 0.],\n",
    " [ 0., 1., 2.]]有两个轴，第一轴就是认为他是一维数组，第一眼看下去，是有两个元素。所以第一轴的长度为2，第二轴的长度为3。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 数组创建"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n",
      "(3, 5)\n",
      "3\n",
      "2\n",
      "int32\n",
      "4\n",
      "15\n",
      "<class 'numpy.ndarray'>\n",
      "[0.   0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1  0.11 0.12 0.13\n",
      " 0.14 0.15 0.16 0.17 0.18 0.19 0.2  0.21 0.22 0.23 0.24 0.25 0.26 0.27\n",
      " 0.28 0.29 0.3  0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4  0.41\n",
      " 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5  0.51 0.52 0.53 0.54 0.55\n",
      " 0.56 0.57 0.58 0.59 0.6  0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69\n",
      " 0.7  0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8  0.81 0.82 0.83\n",
      " 0.84 0.85 0.86 0.87 0.88 0.89 0.9  0.91 0.92 0.93 0.94 0.95 0.96 0.97\n",
      " 0.98 0.99 1.   1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.1  1.11\n",
      " 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.2  1.21 1.22 1.23 1.24 1.25\n",
      " 1.26 1.27 1.28 1.29 1.3  1.31 1.32 1.33 1.34 1.35 1.36 1.37 1.38 1.39\n",
      " 1.4  1.41 1.42 1.43 1.44 1.45 1.46 1.47 1.48 1.49 1.5  1.51 1.52 1.53\n",
      " 1.54 1.55 1.56 1.57 1.58 1.59 1.6  1.61 1.62 1.63 1.64 1.65 1.66 1.67\n",
      " 1.68 1.69 1.7  1.71 1.72 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.8  1.81\n",
      " 1.82 1.83 1.84 1.85 1.86 1.87 1.88 1.89 1.9  1.91 1.92 1.93 1.94 1.95\n",
      " 1.96 1.97 1.98 1.99 2.   2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09\n",
      " 2.1  2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.2  2.21 2.22 2.23\n",
      " 2.24 2.25 2.26 2.27 2.28 2.29 2.3  2.31 2.32 2.33 2.34 2.35 2.36 2.37\n",
      " 2.38 2.39 2.4  2.41 2.42 2.43 2.44 2.45 2.46 2.47 2.48 2.49 2.5  2.51\n",
      " 2.52 2.53 2.54 2.55 2.56 2.57 2.58 2.59 2.6  2.61 2.62 2.63 2.64 2.65\n",
      " 2.66 2.67 2.68 2.69 2.7  2.71 2.72 2.73 2.74 2.75 2.76 2.77 2.78 2.79\n",
      " 2.8  2.81 2.82 2.83 2.84 2.85 2.86 2.87 2.88 2.89 2.9  2.91 2.92 2.93\n",
      " 2.94 2.95 2.96 2.97 2.98 2.99 3.   3.01 3.02 3.03 3.04 3.05 3.06 3.07\n",
      " 3.08 3.09 3.1  3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 3.2  3.21\n",
      " 3.22 3.23 3.24 3.25 3.26 3.27 3.28 3.29 3.3  3.31 3.32 3.33 3.34 3.35\n",
      " 3.36 3.37 3.38 3.39 3.4  3.41 3.42 3.43 3.44 3.45 3.46 3.47 3.48 3.49\n",
      " 3.5  3.51 3.52 3.53 3.54 3.55 3.56 3.57 3.58 3.59 3.6  3.61 3.62 3.63\n",
      " 3.64 3.65 3.66 3.67 3.68 3.69 3.7  3.71 3.72 3.73 3.74 3.75 3.76 3.77\n",
      " 3.78 3.79 3.8  3.81 3.82 3.83 3.84 3.85 3.86 3.87 3.88 3.89 3.9  3.91\n",
      " 3.92 3.93 3.94 3.95 3.96 3.97 3.98 3.99 4.   4.01 4.02 4.03 4.04 4.05\n",
      " 4.06 4.07 4.08 4.09 4.1  4.11 4.12 4.13 4.14 4.15 4.16 4.17 4.18 4.19\n",
      " 4.2  4.21 4.22 4.23 4.24 4.25 4.26 4.27 4.28 4.29 4.3  4.31 4.32 4.33\n",
      " 4.34 4.35 4.36 4.37 4.38 4.39 4.4  4.41 4.42 4.43 4.44 4.45 4.46 4.47\n",
      " 4.48 4.49 4.5  4.51 4.52 4.53 4.54 4.55 4.56 4.57 4.58 4.59 4.6  4.61\n",
      " 4.62 4.63 4.64 4.65 4.66 4.67 4.68 4.69 4.7  4.71 4.72 4.73 4.74 4.75\n",
      " 4.76 4.77 4.78 4.79 4.8  4.81 4.82 4.83 4.84 4.85 4.86 4.87 4.88 4.89\n",
      " 4.9  4.91 4.92 4.93 4.94 4.95 4.96 4.97 4.98 4.99 5.   5.01 5.02 5.03\n",
      " 5.04 5.05 5.06 5.07 5.08 5.09 5.1  5.11 5.12 5.13 5.14 5.15 5.16 5.17\n",
      " 5.18 5.19 5.2  5.21 5.22 5.23 5.24 5.25 5.26 5.27 5.28 5.29 5.3  5.31\n",
      " 5.32 5.33 5.34 5.35 5.36 5.37 5.38 5.39 5.4  5.41 5.42 5.43 5.44 5.45\n",
      " 5.46 5.47 5.48 5.49 5.5  5.51 5.52 5.53 5.54 5.55 5.56 5.57 5.58 5.59\n",
      " 5.6  5.61 5.62 5.63 5.64 5.65 5.66 5.67 5.68 5.69 5.7  5.71 5.72 5.73\n",
      " 5.74 5.75 5.76 5.77 5.78 5.79 5.8  5.81 5.82 5.83 5.84 5.85 5.86 5.87\n",
      " 5.88 5.89 5.9  5.91 5.92 5.93 5.94 5.95 5.96 5.97 5.98 5.99 6.   6.01\n",
      " 6.02 6.03 6.04 6.05 6.06 6.07 6.08 6.09 6.1  6.11 6.12 6.13 6.14 6.15\n",
      " 6.16 6.17 6.18 6.19 6.2  6.21 6.22 6.23 6.24 6.25 6.26 6.27 6.28]\n",
      "[0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259\n",
      " 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317\n",
      " 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081\n",
      " 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992\n",
      " 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903\n",
      " 1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814\n",
      " 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725\n",
      " 2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636\n",
      " 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547\n",
      " 3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458\n",
      " 3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369\n",
      " 4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228\n",
      " 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191\n",
      " 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102\n",
      " 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012\n",
      " 5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923\n",
      " 6.09278575 6.15625227 6.21971879 6.28318531]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(15).reshape(3,5)\n",
    "print(a)\n",
    "print(a.shape)\n",
    "print(len(a))\n",
    "print(a.ndim)\n",
    "print(a.dtype.name)\n",
    "print(a.itemsize)\n",
    "print(a.size)\n",
    "print(type(a))\n",
    "a = np.arange(0,2*np.pi,0.01)\n",
    "print(a)\n",
    "a = np.linspace( 0, 2*np.pi, 100 )\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,)\n",
      "(2, 2)\n",
      "[[[0 0 0 0]\n",
      "  [0 0 0 0]\n",
      "  [0 0 0 0]]\n",
      "\n",
      " [[0 0 0 0]\n",
      "  [0 0 0 0]\n",
      "  [0 0 0 0]]]\n"
     ]
    }
   ],
   "source": [
    "b = np.array([1, 2, 3])\n",
    "print(b.shape)\n",
    "b = np.array([(1,2), [3,4]], dtype=complex)\n",
    "print(b.shape)\n",
    "c = np.empty( (2,3,4), dtype=np.int16)\n",
    "print(c)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 基本操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20 29 38 47]\n",
      "[0 1 4 9]\n",
      "[ True  True False False]\n",
      "[  0  30  80 150]\n",
      "260\n",
      "260\n"
     ]
    },
    {
     "ename": "UFuncTypeError",
     "evalue": "Cannot cast ufunc 'add' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mUFuncTypeError\u001B[0m                            Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-5-d76b1679339d>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      8\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ma\u001B[0m\u001B[1;33m@\u001B[0m\u001B[0mb\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ma\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mdot\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mb\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 10\u001B[1;33m \u001B[0ma\u001B[0m\u001B[1;33m+=\u001B[0m\u001B[1;36m1.2\u001B[0m \u001B[1;31m# 不同类型，无法进行自己修改的操作。\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mUFuncTypeError\u001B[0m: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'"
     ]
    }
   ],
   "source": [
    "# a = np.linspace(20, 51, 4)\n",
    "a = np.arange(20, 51, 10)\n",
    "b = np.arange(4)\n",
    "print(a-b)\n",
    "print(b**2)\n",
    "print(a<35)\n",
    "print(a*b)\n",
    "print(a@b)\n",
    "print(a.dot(b))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "确实不行\n",
      "[2.2 2.2 2.2]\n"
     ]
    }
   ],
   "source": [
    "a = np.ones(3, dtype=np.int32)\n",
    "try:\n",
    "    a+=1.2 # 不同类型，无法进行自己修改的操作。\n",
    "except:\n",
    "    print(\"确实不行\")\n",
    "print(a + 1.2) # 但是这样可以"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1-2.4492935982947064e-16j)\n",
      "(1-2.4492935982947064e-16j)\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "print(np.exp(1j * 2 * np.pi))\n",
    "print(math.e**(1j * 2 *math.pi)) # 没什么区别。但是其实正确答案是1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.14925461 0.24995616 0.39037577]\n",
      " [0.18998301 0.44694293 0.97878445]]\n",
      "2.405296933401626\n",
      "0.14925460811080216\n",
      "0.9787844530445289\n"
     ]
    }
   ],
   "source": [
    "# ndarray类的方法\n",
    "a = np.random.random((2,3))\n",
    "print(a)\n",
    "print(a.sum())\n",
    "print(a.min())\n",
    "print(a.max())\n",
    "# 这三个函数都是幺半群，就是线段树可以用的那种。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[12 15 18 21]\n",
      "(4,)\n",
      "[ 6 22 38]\n",
      "(3,)\n"
     ]
    }
   ],
   "source": [
    "b = np.arange(12).reshape(3,4)\n",
    "print(b)\n",
    "c = b.sum(axis=0)  # 对第一维，也就是一行一行的这一维度，进行sum操作。所以就是所有行加起来，得到只有一行。\n",
    "print(c)\n",
    "print(c.shape)\n",
    "c = b.sum(axis=1) #把每一列加起来，得到只有一列\n",
    "print(c)\n",
    "print(c.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}